<?php
if(!defined('IN_OPEN')) exit('no access');
if(!defined('OPEN_ROOT')) define('OPEN_ROOT',dirname(dirname(dirname(__FILE__))));
/**
 * 天涯服务器端请求辅助操作类
 */
class apiService
{
	public function __construct()
	{
		
	}
	public function request($url,$param=null,$method='get',$cookie='')
	{
		//print_r($param);
		if($method=='get')
		{
			$send_data.= http_build_query($param);
			if(eregi('\?',$url))
			{
				$url.= '&'.$send_data;
			}
			else
			{
				$url.= '?'.$send_data;
			}
			
		}
		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL, $url); 
		if(!empty($cookie))
		{
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($method=='post')
		{
			$send_data = $param;
			curl_setopt($ch, CURLOPT_POST, 1);
			//添加变量
			curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
		}
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
		$MySources = curl_exec ($ch); 
		curl_close($ch); 
		return $MySources; 
	}
	public  function jsonToXml($arr,$encoding)
	{
		$arr = json_decode($arr,true);
		//print_r($arr);
		$xml='<?xml version="1.0" encoding="'.$encoding.'"?>';
		//生成
		$xml.=$this->array2xml($arr);
		return $xml;
		
	}
	private function array2xml($array)
	{
		foreach($array as $key=>$val) 
		{
			is_numeric($key)&&$key="item id=\"$key\"";
			$xml.="<$key>";
			$xml.=is_array($val)?$this->array2xml($val):$val;
			list($key,)=explode(' ',$key);
			$xml.="</$key>";
		}
		return $xml;
		
	}
	
} 
?>